﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace AG.Data
{
    public class AGDataLoader
    {
        public AGData Load(string dataFile)
        {
            AGData data = new AGData();
            XDocument xDoc = XDocument.Load(dataFile);

            XElement xData = xDoc.Element("data");

            #region 模型的类别信息不从data文件读取，改成model-category读取
            /*
            List<XElement> xModelCategoryList = xData.Element("model-category-list").Elements("category").ToList();
            for (int index = 0; index < xModelCategoryList.Count; index++)
            {
                XElement xModelCagetgory = xModelCategoryList[index];
                AGModelCategory category = new AGModelCategory();
                category.Id = Convert.ToByte(xModelCagetgory.Attribute("id").Value);
                category.Caption = xModelCagetgory.Attribute("caption").Value;
                data.ModelCategoryList.Add(category);
            }*/
            #endregion

            List<XElement> xModelList = xData.Element("model-list").Elements("model").ToList();
            for (int index = 0; index < xModelList.Count; index++)
            {
                XElement xModel = xModelList[index];
                AGModel model = new AGModel();
                model.Id = (ushort)Convert.ToInt16(xModel.Attribute("id").Value);
                model.Caption = xModel.Attribute("caption").Value;
                model.CategoryId = (ushort)Convert.ToInt16(xModel.Attribute("category-id").Value);
                data.ModelList.Add(model);
            }

            List<XElement> xTerrainList = xData.Element("terrain-list").Elements("terrain").ToList();
            for (int index = 0; index < xTerrainList.Count; index++)
            {
                XElement xTerrain = xTerrainList[index];
                AGTerrain terrain = new AGTerrain();
                terrain.Id = Convert.ToByte(xTerrain.Attribute("id").Value);
                terrain.ModelId = Convert.ToUInt16(xTerrain.Attribute("model-id").Value);
                terrain.ForegroundId = Convert.ToByte(xTerrain.Attribute("foreground").Value);
                terrain.BackgroundId = Convert.ToByte(xTerrain.Attribute("background").Value);
                terrain.Caption = xTerrain.Attribute("caption").Value;

                data.TerrainList.Add(terrain);

                List<XElement> xTileList = xTerrain.Elements("tile").ToList();
                for (int tileIndex = 0; tileIndex < xTileList.Count; tileIndex++)
                {
                    XElement xTile = xTileList[tileIndex];
                    AGTerrainTile tile = new AGTerrainTile();
                    terrain.Tiles.Add(tile);
                    tile.Caption = xTile.Attribute("caption").Value;
                    tile.Id = (byte)Convert.ToInt16(xTile.Attribute("id").Value);
                    tile.Count = (byte)Convert.ToInt16(xTile.Attribute("count").Value);
                }
            }

            return data;
        }

        public void LoadUnitList(AGData data, string unitPlistFile)
        {
            XDocument xDoc = XDocument.Load(unitPlistFile);

            List<XElement> xUniteCategoryList = xDoc.Element("unit-list").Element("unit-category-list").Elements("unit-category").ToList();
            for (int index = 0; index < xUniteCategoryList.Count; index++)
            {
                XElement xCategory = xUniteCategoryList[index];

                AGUnitCategory category = new AGUnitCategory();
                category.Id = (ushort)Convert.ToInt16(xCategory.Attribute("id").Value);
                category.Caption = xCategory.Attribute("caption").Value;

                data.UnitCategoryList.Add(category);
            }

            List<XElement> xUnitList = xDoc.Element("unit-list").Elements("unit").ToList();
            for (int index = 0; index < xUnitList.Count; index++)
            {
                XElement xUnit = xUnitList[index];

                AGUnit unit = new AGUnit();
                unit.Id = (ushort)Convert.ToInt16(xUnit.Attribute("id").Value);
                unit.Caption = xUnit.Attribute("caption").Value;
                unit.CategoryId = (ushort)Convert.ToInt16(xUnit.Attribute("category-id").Value);
                unit.ModelId = (ushort)Convert.ToInt16(xUnit.Attribute("model-id").Value);

                data.UnitList.Add(unit);
            }
        }

        public void LoadUnitCategoryList(AGData data, string unitUnitCategoryListFile)
        {
            XDocument xDoc = XDocument.Load(unitUnitCategoryListFile);

            List<XElement> xCategoryList = xDoc.Element("unit-category-list").Elements("unit-category").ToList();
            for (int index = 0; index < xCategoryList.Count; index++)
            {
                XElement xCategory = xCategoryList[index];

                AGUnitCategory category = new AGUnitCategory();
                category.Id = (ushort)Convert.ToInt16(xCategory.Attribute("id").Value);
                category.Caption = xCategory.Attribute("caption").Value;

                data.UnitCategoryList.Add(category);
            }
        }

        public void LoadObjectCategoryList(AGData data, string objectCategoryListFile)
        {
            XDocument xDoc = XDocument.Load(objectCategoryListFile);

            List<XElement> xCategoryList = xDoc.Element("object-category-list").Elements("object-category").ToList();
            for (int index = 0; index < xCategoryList.Count; index++)
            {
                XElement xCategory = xCategoryList[index];

                AGMapObjectCategory category = new AGMapObjectCategory();
                category.Id = (byte)Convert.ToInt16(xCategory.Attribute("id").Value);
                category.Caption = xCategory.Attribute("caption").Value;

                data.ObjectCategoryList.Add(category);
            }
        }

        public void LoadModelCategoryList(AGData data, string modelCategoryListFile)
        {
            XDocument xDoc = XDocument.Load(modelCategoryListFile);

            List<XElement> xCategoryList = xDoc.Element("model-category-list").Elements("model-category").ToList();
            for (int index = 0; index < xCategoryList.Count; index++)
            {
                XElement xCategory = xCategoryList[index];

                AGModelCategory category = new AGModelCategory();
                category.Id = Convert.ToInt16(xCategory.Attribute("id").Value);
                category.Caption = xCategory.Attribute("caption").Value;

                data.ModelCategoryList.Add(category);

                List<XElement> xActionList = xCategory.Element("action-definition").Elements("action").ToList();
                for (int aIndex = 0; aIndex < xActionList.Count; aIndex++)
                {
                    XElement xAction = xActionList[aIndex];
                    AGModelCategoryAction action = new AGModelCategoryAction();
                    action.Id = Convert.ToInt16(xAction.Attribute("id").Value);
                    action.Caption = xAction.Attribute("caption").Value;
                    category.Actions.Add(action);
                }

                List<XElement> xDirectionList = xCategory.Element("direction-definition").Elements("direction").ToList();
                for (int dIndex = 0; dIndex < xDirectionList.Count; dIndex++)
                {
                    XElement xDirection = xDirectionList[dIndex];
                    AGModelCategoryDirection direction = new AGModelCategoryDirection();
                    direction.Id = Convert.ToInt16(xDirection.Attribute("id").Value);
                    direction.Caption = xDirection.Attribute("caption").Value;
                    category.Directions.Add(direction);
                }
            }
        }
    }
}
